/********************************
 * @FileName:     BooLeanTypeHandler.java
 * @Description:  BooLeanTypeHandler.java -
 * @Copyright:    Copyright(C) 2010-2011 mbool.com
 * @Company       MBOOL-TEAM zhangjiayi LTD.
 * @author:       zhangjiayi
 * @version       V1.0 
 * @Createdate:   2016-5-9 下午10:39:01
 *
 * All rights Reserved, Designed By MBOOL-TEAM
 *
 * Modification  History:
 * Date         Author        Version        Discription
 * -----------------------------------------------------------------------------------
 * 2016-5-9       zhangjiayi       1.0         TODO
 * Why & What is modified: <修改原因描述>
 *
 ********************************/
package com.rowline.admin.module.util.data.ibatis;

import com.rowline.admin.module.util.constant.ContextContants;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * @ClassName:     BooLeanTypeHandler
 * @Description:   mybatis 中的char跟javabean中的Boolean类型转换,
 * 				  如果为boolean自动转换为Y N
 * @author:        zhangjiayi
 * @date:          2016-5-9 下午10:39:01
 *
 *
 */
@MappedJdbcTypes(JdbcType.CHAR)
public class BooleanTypeHandler extends BaseTypeHandler<Boolean> {


	@Override
	public Boolean getNullableResult(ResultSet rs, String columnName) throws SQLException {
		String re = rs.getString(columnName);
		if(re==null){
			return null;
		}
		return ContextContants.YES.equals(re)?true:false;
	}

	@Override
	public Boolean getNullableResult(ResultSet rs, int columnName) throws SQLException {
		String re = rs.getString(columnName);
		if(re==null){
			return null;
		}
		return ContextContants.YES.equals(re)?true:false;
	}

	@Override
	public Boolean getNullableResult(CallableStatement rs, int columnName) throws SQLException {
		String re = rs.getString(columnName);
		if(re==null){
			return null;
		}
		return ContextContants.YES.equals(re)?true:false;
	}

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType)
			throws SQLException {
		
		if(parameter != null ){
			ps.setString(i, parameter? ContextContants.YES :ContextContants.NO);
		}
		
	}

	

}
